package com.ruoyi.web.controller.danger;

import com.alibaba.fastjson.JSON;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.system.entity.OneElectronFenceAlarm;
import com.ruoyi.system.entity.OneElectronFenceStatus;
import com.ruoyi.system.service.OneElectronFenceAlarmService;
import com.ruoyi.system.service.OneElectronFenceStatusService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("PushAlarm")
public class PushAlarmController {
    @Resource
    private RedisCache redisCache;

    @Autowired
    private OneElectronFenceAlarmService oneElectronFenceAlarmService;
    @Autowired
    private OneElectronFenceStatusService oneElectronFenceStatusService;

    /*

        1.报警码转换
        2.定时任务时间调长
        3.对接报警状态

     */

    @PostMapping("/DeviceStatus")
    public AjaxResult deviceStatus(@RequestParam Map<Object, Object> map) {
        redisCache.setCacheObject("一号防区数据", JSON.toJSONString(map));
        return AjaxResult.success();
    }

    @PostMapping("/SectorStatus")
    public AjaxResult sectorStatus(@RequestParam Map<Object, Object> map) {
        //报警主机发送设备通道状态
        log.info("报警主机发送设备通道状态:{}", JSON.toJSONString(map));

        oneElectronFenceStatusService.delete();

        String param = map.get("param").toString();
        Map parseObject = JSON.parseObject(param, Map.class);

        List<Map> sectorStatus = (List<Map>) parseObject.get("SectorStatus");
        for (Map var : sectorStatus) {
            OneElectronFenceStatus oneElectronFenceStatus = new OneElectronFenceStatus();
            oneElectronFenceStatus.setDeviceId(parseObject.get("DeviceId").toString());
            oneElectronFenceStatus.setArmingStatus(parseObject.get("ArmingStatus").toString());
            oneElectronFenceStatus.setDeviceType(parseObject.get("DeviceType").toString());
            oneElectronFenceStatus.setChannelCount((Integer) parseObject.get("ChannelCount"));
            oneElectronFenceStatus.setSector((Integer) var.get("Sector"));
            oneElectronFenceStatus.setSectorStatus((Integer) var.get("Status"));
            oneElectronFenceStatus.setCreatedBy("sys");
            oneElectronFenceStatus.setCreatedDate(new Date());
            oneElectronFenceStatusService.insert(oneElectronFenceStatus);
        }

        return AjaxResult.success();
    }

    @PostMapping("/AlarmEvent")
    public AjaxResult alarmEvent(@RequestParam Map<Object, Object> map) {
        //报警主机发送报警事件
        log.info("报警主机发送报警事件:{}", JSON.toJSONString(map));

        String param = map.get("param").toString();
        Map parseObject = JSON.parseObject(param, Map.class);

        OneElectronFenceAlarm oneElectronFenceAlarm = new OneElectronFenceAlarm();
        oneElectronFenceAlarm.setDeviceId(parseObject.get("DeviceId").toString());
        oneElectronFenceAlarm.setAlarmTime(parseObject.get("AlarmTime").toString());
        oneElectronFenceAlarm.setChannel(parseObject.get("Channel").toString());
        oneElectronFenceAlarm.setSubChannel(parseObject.get("SubChannel").toString());
        oneElectronFenceAlarm.setCld(parseObject.get("CID").toString());
        oneElectronFenceAlarm.setCreatedBy("sys");
        oneElectronFenceAlarm.setCreatedDate(new Date());

        oneElectronFenceAlarmService.insert(oneElectronFenceAlarm);

        return AjaxResult.success();
    }
}
